﻿using Domain;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Api
{
    public class LockersService : BaseService<Lockers>
    {
        private static LockersService instance;
        private static object _lock = new object();
        private LockersService() { }

        /// <summary>
        /// 获取实体类
        /// </summary>
        /// <returns></returns>
        public static LockersService getInstance()
        {
            if (instance == null)
            {
                lock (_lock)
                {
                    if (instance == null)
                    {
                        instance = new LockersService();
                        return instance;
                    }
                }
            }
            return instance;
        }
        /// <summary>
        /// 存物 更新 柜子状态
        /// </summary>
        /// <param name="card_no"></param>
        /// <param name="locker_no"></param>
        public void store_update(string card_no,string locker_no) {
            if (card_no == null || card_no == "" || locker_no == "" || locker_no == null) {
                throw new ArgumentException("card_no 未空 或者 locker_no 为空");
            }
            string sql = "UPDATE m_lockers SET STATUS = 2 ,card_no = 'card_no_date' WHERE lockers_no='locker_no_data'";
            sql=sql.Replace("card_no_date", card_no).Replace("locker_no_data",locker_no);
            updateBySql(sql);
        }

        /// <summary>
        ///  取物 更新 柜子状态
        /// </summary>
        /// <param name="card_no"></param>
        /// <param name="locker_no"></param>
        public void unstore_update(string card_no,string locker_no) {
            if (card_no == null || card_no == "" || locker_no == "" || locker_no == null)
            {
                throw new ArgumentException("card_no 未空 或者 locker_no 为空");
            }
            string sql = "UPDATE m_lockers SET STATUS = 1 ,card_no = NULL WHERE lockers_no='locker_no_data'";
            sql = sql.Replace("card_no_date", card_no).Replace("locker_no_data", locker_no);
            updateBySql(sql);
        }
        /// <summary>
        /// 根据指纹柜获取 锁柜 信息
        /// </summary>
        /// <returns></returns>
        public Lockers getLockers(string lockers_no)
        {
            if (lockers_no == null || lockers_no == "") {
                throw new ArgumentNullException("lockers_no 为空");
            }
            string sql = "select * from m_lockers where lockers_no='?'".Replace("?", lockers_no);
            var lockers= findBySql(sql);
            if (lockers == null) {
                throw new NullReferenceException("根据lockers_no  无法找到柜子，此 lockers_no 不存在");
            }
            return lockers;
        }

        /// <summary>
        /// 根据卡号获取 锁柜 信息
        /// </summary>
        /// <returns></returns>
        public Lockers getLockersByCardNo(string card_no)
        {
            if (card_no == null || card_no == "")
            {
                throw new ArgumentNullException("card_no 为空");
            }
            string sql = "select * from m_lockers where card_no='?'".Replace("?", card_no);
            var lockers = findBySql(sql);
            return lockers;
        } 


        /// <summary>
        /// 判断是存 还是取
        /// </summary>
        /// <param name="card_no">卡编号</param>
        /// <returns></returns>
        public int is_in_out(string card_no)
        {
            string sql = "select count(id) as count from m_lockers where card_no=?";
            object o = uniqResult(sql, new object[] { card_no });
            if (o == null||o.ToString()=="0")
            {//没有存东西
                return 0;//标识可存
            }
            else
            {
                return 1;
            }

        }

    }
}
